package com.light.common.mybatisplus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.light.common.base.BaseQuery;
import org.apache.commons.lang.StringUtils;

/**
 * @Author YHR
 * @Date 2021/2/7 14:47
 * @Version 1.0
 */
public class MybatisPlusHelper {

    public static QueryWrapper setQueryWrapperPageParam(QueryWrapper queryWrapper, BaseQuery baseQuery) {
        String sort = baseQuery.getSort();
        if (StringUtils.isNotBlank(sort)) {
            if (StringUtils.isBlank(baseQuery.getOrder())) {
                queryWrapper.orderByAsc(sort);
            }
            queryWrapper.orderByDesc(sort);
        }
        baseQuery.setOffset((baseQuery.getCurrentPage() - 1) * baseQuery.getLimit());
        queryWrapper.last(String.format("limit %s , %s", baseQuery.getOffset(), baseQuery.getLimit()));
        return queryWrapper;
    }
}
